<template>
  <scroll
   class="user-list"
   :probe-type="3"
   @scroll="onScroll"
   ref="scrollRef"
  >
    <ul ref="groupsRef" class="list-wrapper" v-if="data && data.length > 0">
      <li v-for="(group, i) in data" v-bind:key="i">
        <p class="group-tit">{{group.title}}</p>
        <ul>
          <li v-for="(item, ii) in group.list" v-bind:key="ii" class="group-item" @click="handleSelect(item)">
            <avatar :arr="[]" />
            <p>{{item.name}}</p>
          </li>
        </ul>
      </li>
    </ul>
    <div
    class="fixed"
    v-if="fixedTitle"
    :style="fixedStyle"
    >
      <div class="group-tit">{{fixedTitle}}</div>
    </div>
    <div
    class="fixed-right"
    @touchstart.stop.prevent="onShortcutTouchStart"
    @touchmove.stop.prevent="onShortcutTouchMove"
    @touchend.stop.prevent
    >
      <ul v-if="data && data.length > 0">
        <li class="f-r-i" v-for="(groupTit, i) in shortcutList" v-bind:key="i" :data-index="i" :class="currentIndex === i ? 'active' : ''">{{groupTit}}</li>
      </ul>
    </div>
  </scroll>
</template>
<script>
import Scroll from '@/components/wrapScroll'
import useFixed from './use-fixed'
import useShortcut from './use-shortcut'
export default {
  name: 'UserList',
  components: { Scroll },
  props: {
    data: Array
  },
  emits: ['select'],
  setup(props, { emit }) {
    const { onScroll, groupsRef, fixedTitle, fixedStyle, currentIndex } = useFixed(props)
    const { shortcutList, scrollRef, onShortcutTouchStart, onShortcutTouchMove } = useShortcut(props, groupsRef)
    function handleSelect(user) {
      emit('select', user)
    }
    return {
      fixedTitle,
      groupsRef,
      fixedStyle,
      currentIndex,
      shortcutList,
      scrollRef,
      onShortcutTouchStart,
      onShortcutTouchMove,
      onScroll,
      handleSelect
    }
  }
}
</script>
<style lang="scss" scoped>
  .user-list {
    position: relative;
    overflow: hidden;
    height: calc(100% - 64px);
    .list-wrapper {
      padding-right: 18px;
    }
  }
  .group-item {
    color: $color-text-ll;
    font-size: 14px;
    padding: 12px 10px;
    line-height: 38px;
    border-bottom: 1px solid $color-background-d;
    &:last-of-type {
      border-bottom: none;
    }
    .avatar-wrapper {
      float: left;
      margin-right: 8px;
    }
  }
  .group-tit {
    padding-left: 12px;
    box-sizing: border-box;
    height: 26px;
    line-height: 26px;
    font-size: 14px;
    color: $color-text-ll;
    background-color: $color-dialog-background;
  }
  .fixed {
    width: 100%;
    position: absolute;
    top: -1px;
    left: 0;
  }
  .fixed-right {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 18px;
    .f-r-i {
      line-height: 18px;
      height: 18px;
      text-align: center;
      font-size: 12px;
      color: $color-text-ll;
      &.active {
        color: $color-theme;
      }
    }
  }
</style>
